﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.BLL
{
    class InternetService
    {
        //Ham gui Request bang phuong thuc Get
        public Stream GetHtmlStreamGetMethod(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.UserAgent = "Mozilla/5.0";
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return response.GetResponseStream();
            }
            catch (Exception)
            {
                return null;
            }
        }

        //Ham gui Request bang phuong thuc Post co chua Form Data
        public Stream GetHtmlStreamPostMethod(string url)
        {
            WebClient wc = new WebClient();
            //Thong Tin cua Header
            wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
            //wc.Headers["Cookie"] = "__utma=31260783.1257323130.1379434877.1379758919.1379844307.10; __utmb=31260783.14.10.1379844307; __utmc=31260783; __utmz=31260783.1379434877.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)";

            //postData la bien chua Form Data
            System.Collections.Specialized.NameValueCollection postData = new System.Collections.Specialized.NameValueCollection();
            postData.Add("data[start_date]", "23/09/2013");//DateTime.Today.AddDays(1).ToString("dd/MM/yyyy")
            postData.Add("data[end_date]", "23/09/2013");

            try
            {
                Stream html = new MemoryStream(wc.UploadValues(url, "POST", postData));
                return html;
            }
            catch (Exception)
            {
                return null;
            }


        }
    }
}
